package cn.skylark.wcm.common.scheduler;

import cn.skylark.wcm.dao.FileTableDao;
import cn.skylark.wcm.entity.Filetable;
import cn.skylark.wcm.service.FileUploadService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class ScheduledJob1 implements Job {
    @Autowired
    FileUploadService fileUploadService;

    @Autowired
    FileTableDao fileTableDao;
    /** logger */
    private static final Logger LOGGER = LoggerFactory.getLogger(ScheduledJob1.class);
    /**
     * 定时任务(获取时间段的txt上传至后台服务器)
     * 业务:定时任务时间与addDate(oldTime，x) 中的x保持一致
     * x单位为秒，获取定时任务每次执行间，数据库中新生成的txt文件，
     * 将txt文件上传至后台服务器指定目录下，相同租户的txt放在相同
     * 租户文件夹内
     *
     * @param arg0
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        LOGGER.info( "向supersearch服务器推送文件开始！" );
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String newTime = dateFormat.format(now);
        String oldTime = ScheduledJob1.addDate(newTime, 60);
        Example example = new Example(Filetable.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("suffix", "txt");
        criteria.andEqualTo("type", 3);
        criteria.andBetween("updatetime", oldTime, newTime);
        List<Filetable> filetableMessage = fileTableDao.selectByExample(example);
        if (filetableMessage.size() > 0) {
            LOGGER.info( "向supersearch服务器推送txt文件开始！" );
            //dev
            fileUploadService.downloadTxT(filetableMessage, "dev");
//            //prod
//            fileUploadService.downloadTxT(filetableMessage, "prod");
            LOGGER.info( "向supersearch服务器推送txt文件结束！" );
        }
        Example examplehtml = new Example(Filetable.class);
        Example.Criteria criteriahtml = examplehtml.createCriteria();
        criteriahtml.andEqualTo("type", 1);
        criteriahtml.andEqualTo("suffix", "html");
        criteriahtml.andBetween("updatetime", oldTime, newTime);
        List<Filetable> filetableMessageHtml = fileTableDao.selectByExample(examplehtml);
        if (filetableMessageHtml.size() > 0) {
            LOGGER.info( "向服务器推送html文件开始！" );
            //dev
            fileUploadService.downloadHTML(filetableMessageHtml, "dev");
            //prod
//            //111.230.226.219
//            fileUploadService.downloadHTML(filetableMessageHtml, "219");
//            //118.126.91.232
//            fileUploadService.downloadHTML(filetableMessageHtml, "232");
            LOGGER.info( "向服务器推送html文件结束！" );
        }
        LOGGER.info( "向supersearch服务器推送文件结束！" );
    }

    /**
     * 获取x秒后的时间
     *
     * @param day 当前系统时间
     * @param x   设置的秒数
     * @return
     */
    public static String addDate(String day, int x) {
        SimpleDateFormat fromat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = fromat.parse(day);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (date == null) {
            return "";
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.SECOND, -x);
        date = cal.getTime();
        return fromat.format(date);
    }
}
